/**
 * @FileName: Job.java
 * @Author
 * @Description:
 * @Date 2016年3月15日 上午11:07:25
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.service.ds;

import java.util.Date;
import java.util.Properties;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.config.Plugins;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;

import cn.hpclub.server.model.SalesPromotion;
import cn.hpclub.server.service.ds.DateUtils.SyncParams;

public class JobByBusinessCount implements Job{
    private static Logger logger = LoggerFactory.getLogger(SalesPromotion.class);

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException{
        logger.debug("JobByBusinessCount.execute");
        exportSalesPromotion();
    }

    public void exportTxt(){
        exportSalesPromotion();
    }

    private void exportSalesPromotion(){
        logger.debug("exportSalesPromotion.execute");
        if(DsPropKit.getProp().getBoolean(DsPropKit.SWITCH_BUSINESS_COUNT_MONTHLY_EXPORT)){
            SyncParams params = DateUtils.getLastMonthParams(new Date());
            params.logTag = "统计业务办理量数据(上月)";
            new ExportBusinessCountTextData(params).exportAndUpload();
        } else{
            DsLog.log("未开启[每月导出业务办理量统计]功能，放弃任务.");
        }
    }

    public static void main(String[] args) throws Exception{
        Prop prop = PropKit.use("jdbc.properties", "utf-8");
        Properties props = prop.getProperties();

        Plugins me = new Plugins();
        String url = props.getProperty("jdbcUrl");
        String username = props.getProperty("user");
        String password = props.getProperty("password");
        String driverClass = props.getProperty("driverClass");
        String filters = props.getProperty("filters");
        int initialSize = 10;
        int minIdle = 20;
        int maxActive = 50;
        long maxWait = 50;

        // mysql 数据源
        DruidPlugin dsMysql = new DruidPlugin(url, username, password, driverClass, filters);
        dsMysql.set(initialSize, minIdle, maxActive);
        dsMysql.setMaxWait(maxWait);
        me.add(dsMysql);
        dsMysql.start();
        ActiveRecordPlugin arpMysql = new ActiveRecordPlugin("mysql", dsMysql);
        me.add(arpMysql);
        arpMysql.start();
        JobByBusinessCount jobByBusinessCount = new JobByBusinessCount();
        jobByBusinessCount.exportSalesPromotion();
    }

}
